package com.itwn.portal.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itwn.portal.model.entity.Book;
import com.itwn.portal.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/book")
@Api(tags = "图书模块")
public class BookController {
    // 依赖注入，通过构造器注入； 相当于 @Authowired
    private final BookService bookService;
    public BookController(BookService bookService) {
        this.bookService = bookService;
    }

    @PostMapping("/top")
    @ApiOperation("热销榜")
    public ResponseEntity<IPage<Book>> findById(@RequestBody Map<String,Integer> params) {
        Integer pageNum = params.get("pageNum");
        Integer pageSize = params.get("pageSize");
        IPage<Book> pages = bookService.findTopSales(pageNum, pageSize);
        return ResponseEntity.ok(pages);
    }

    @PostMapping("/findByType")
    @ApiOperation("根据类别查询书籍")
    public ResponseEntity<IPage<Book>> findByType(@RequestBody Map<String,Integer> params) {
        Integer pageNum = params.get("pageNum");
        Integer pageSize = params.get("pageSize");
        Integer typeId = params.get("typeId");
        IPage<Book> pages = bookService.findByType(pageNum, pageSize, typeId);
        return ResponseEntity.ok(pages);
    }

    /**
     * 通过ID查询书籍
     */
    @GetMapping("/findById/{id}")
    @ApiOperation("通过ID查询书籍")
    public ResponseEntity<Book> findById(@PathVariable("id") Long id) {
        Book book = bookService.findById(id);
        return ResponseEntity.ok(book);
    }
}
